// 菜单定义

const { app, dialog } = require('electron');
const { isDev, alert, error, getMainWindow, openModal, openUrl, openLink } = require('./utils')
const prompt = require('electron-prompt');

const template = [
    {
        label: '文件',
        submenu: [
            {
                label: '新建项目'
            },
            {
                label: '系统选项',
                id: 'systemoption',
            },
            {
                label: '系统授权',
                id: 'systemoption',
            },
            {
                type: 'separator'
            },
            {
                label: '退出',
                click: function () {
                    dialog.showMessageBox({
                        type: 'warning',
                        buttons: ['确定', '取消'],
                        title: '提示',
                        message: '确定要退出吗？'
                    }).then(resp => {
                        // resp=>{ response: 0, checkboxChecked: false }
                        if (resp.response === 0) {
                            app.quit();
                        }
                    });
                }
            }
        ]
    },
    {
        label: '查看',
        submenu: [
            {
                label: '放大',
                role: 'zoomIn'
            },
            {
                label: '缩小',
                role: 'zoomOut'
            },
            {
                label: '重置缩放',
                role: 'resetZoom'
            },
            {
                label: '切换全屏',
                role: 'togglefullscreen',
            },
            {
                label: '重新载入',
                role: 'forceReload'
            }
        ]
    },
    {
        label: '工具',
        submenu: [
            {
                label: '数据库设置',
                click: function () {
                    openModal({
                        fileName: 'dwz_jui-master/dbsettings.html',
                        height: 210,
                        modal: true,
                        // devTools: true,
                    });
                }
            },
            {
                label: '数据采集工具'
            },
            {
                label: '系统环境检查',
                click: function () {
                    let mainWindow = getMainWindow();
                    openModal({
                        fileName: 'sysinfo.html',
                        parentWindow: mainWindow,
                        width: 450,
                        height: 550,
                        minimizable: false
                    });
                }
            },
            {
                type: 'separator'
            },
            {
                label: '打开网址',
                id: 'openUrl',
                click: () => {
                    prompt({
                        height: 180,
                        title: 'Go to URL',
                        label: 'URL:',
                        value: 'https://',
                        inputAttrs: {
                            type: 'url'
                        },
                        type: 'input'
                    })
                        .then((r) => {
                            if (r === null) {

                            } else {
                                console.log(r);
                                // 判断是否为空白字符
                                if (r.trim() === '') return;
                                openUrl({
                                    url: r, parentWindow: null, modal: false, width: 800, height: 600, minimizable: true, resizable: true
                                });
                            }
                        })
                        .catch(console.error);

                }
            }
        ]
    }
    ,
    {
        label: '帮助',
        submenu: [
            {
                label: '在线帮助'
            },
            {
                type: 'separator'
            },
            {
                label: '开发者工具',
                id: 'devtools',
                click: () => {
                    if (isDev) {
                        let mainWindow = getMainWindow();
                        mainWindow && mainWindow.webContents.openDevTools();
                    } else {
                        error('当前版本不支持开发者工具', '提示');
                    }
                }
            },
            {
                label: '联系我们',
                click: function () { openLink('https://www.yonyouaud.com') }
            },
            {
                label: '版本检查',
                id: 'checkversion',
            },
            {
                type: 'separator'
            },
            {
                label: '关于',
                id: 'about',
                click: function () {
                    let mainWindow = getMainWindow();
                    openModal({
                        fileName: 'about.html',
                        parentWindow: mainWindow,
                        width: 360,
                        height: 340
                    });
                }
            }
        ]
    }
];

module.exports = template;